package com.jewelry.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jewelry.common.util.Constants;
import com.jewelry.common.util.MD5Util;
import com.jewelry.dao.IUserDAO;
import com.jewelry.service.IConfigAttrService;
import com.jewelry.service.IUserService;
import com.jewelry.vo.StatisticsVO;
import com.jewelry.vo.UserVO;
import com.jewelry.web.vo.UserSession;

public class UserServiceImpl implements IUserService {

	private static Logger logger = LoggerFactory
			.getLogger(UserServiceImpl.class);

	private IUserDAO userDAO;

	public IUserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	private IConfigAttrService configAttrService;

	public IConfigAttrService getConfigAttrService() {
		return configAttrService;
	}

	public void setConfigAttrService(IConfigAttrService configAttrService) {
		this.configAttrService = configAttrService;
	}
	
	//========================================================================

	public String getLoginResult(Map<String, Object> session) {
		UserVO userVO = getUserInfoFromSession(session);
		switch (userVO.getUserType()) {
		case 1:
			return "a";
		case 2:
			return "o";
		case 3:
			return "f";
		default:
			return "a";
		}

	}

	@Override
	public boolean doLogin(UserVO userVO, Map<String, Object> session) {
		if (logger.isDebugEnabled()) {
			logger.debug("enter doLogin ... ");
		}
		if (userVO == null) {
			return false;
		}
		// 密码加密
		String primitivePasswd = userVO.getPasswd();
		String md5Passwd = MD5Util
				.md5(primitivePasswd, Constants.ENCODING_CODE);
		userVO.setPasswd(md5Passwd);

		UserVO temp = null;
		try {
			temp = userDAO.getUserForLogin(userVO);
		} catch (Exception e) {
			logger.error("getUserForLogin error", e);
		}
		if (temp == null) {
			return false;
		} else {
			putUserInfo2Session(session, temp);
			return true;
		}
	}

	@Override
	public void putUserInfo2Session(Map<String, Object> session, UserVO userVO) {
		UserSession userSession = new UserSession();
		userSession.setUserVO(userVO);
		session.put(Constants.USER_INFO, userSession);
	}

	@Override
	public UserVO getUserInfoFromSession(Map<String, Object> map) {
		UserSession session = (UserSession) map.get(Constants.USER_INFO);
		return session.getUserVO();
	}

	@Override
	public void addUser(UserVO user) {
		String md5Passwd = MD5Util.md5("123123", Constants.ENCODING_CODE); //获取用户输入的密码MD5
		user.setPasswd(md5Passwd);
		userDAO.addUser(user);
	}

	@Override
	public List<StatisticsVO> getGroupByUserType() {
		Map<Integer,String> map = configAttrService.getConfigAttrMapByConfigGroup(Constants.USER_TYPE_COONFIG); 
		List<StatisticsVO> result = userDAO.getGroupByUserType();
		
		for(StatisticsVO vo: result){
			vo.setTypeName(map.get(vo.getType()));
		}
		return result;
	}

	@Override
	public List<UserVO> geteUserListByPage(UserVO user) {
		int totalCount = userDAO.getUserCount(user);
		user.getPage().setTotalItem(totalCount);
		List<UserVO> list = userDAO.geteUserListByPage(user);
		Map<Integer,String> map = configAttrService.getConfigAttrMapByConfigGroup(Constants.USER_TYPE_COONFIG);
		for(UserVO vo:list){
			vo.setUserTypeName(map.get(vo.getUserType()));
		}
		return list;
	}
	
	/**
	 * @return 0 正常  1 原密码错误
	 */
	public boolean modifyPasswd(UserVO user){
		UserVO temp = userDAO.getUserByUserid(user.getUserid()); //获取数据库存储用户信息
		String md5Passwd = MD5Util.md5(user.getPasswd(), Constants.ENCODING_CODE); //获取用户输入的密码MD5
		if(!temp.getPasswd().equals(md5Passwd)){
			return false;
		}
		md5Passwd = MD5Util.md5(user.getNewPasswd(), Constants.ENCODING_CODE); //获取用户输入的新密码MD5
		user.setPasswd(md5Passwd);
		userDAO.passwdUpdate(user);
		return true;
	}
	
	public boolean isLoginidExist(String loginid){
		UserVO user = userDAO.getUserByLoginid(loginid);
		return user==null?false:true;
	}

}
